@echo off
setlocal ENABLEDELAYEDEXPANSION
set up_dir=C:\lebo\build_bat
set apk_dir=C:\lebo\apk_lebo\apk_upgrade_test
set pc_dir=C:\lebo\apk_lebo\pc_upgrade_test
set release_dir=C:\lebo\apk_lebo\release_note
set local_sign_path=C:\lebo\apk_lebo\sender_sign_apk
set apk2cdn_path=C:\lebo\apk_lebo\apk_to_cdn
set ftp_sign_path=\\192.168.8.251\ftp\output\Sign_apk\app_for_anl
set ftp_signed_apk=ftp://192.168.8.251/output/Sign_apk/app_for_anl
set cur_path=%cd%
set ftp_url=http://download.mirrorcast.cn/test
set ftp_url_release=http://download.mirrorcast.cn/release
set md5sum="C:\Program Files\Git\usr\bin\md5sum.exe"
set sed="C:\Program Files\Git\usr\bin\sed.exe"
set zip="C:\Program Files\7-Zip\7z.exe"
set datevar=%date:~0,4%%date:~5,2%%date:~8,2%
set min=%time:~3,2%
Set hour=%time:~0,2%
if %hour% leq 9 ( set hour=0%hour:~1,1%)
set mytime=%hour%%min%
set result_dir=%datevar%%mytime%

:: %1 ---> android app 的仓库名
:: %2 ---> 发送端app签名功能
:: %3 ---> pc exe 上传到ftp
:: %4 ---> apk 上传到cdn
:: %5 ---> app 升级功能测试
:: %6 ---> 上传版本发布说明到ftp版本发布路径下

set app_repo=%1
set send_apk_sign=%2
set pc_exe=%3
set apk2cdn=%4
set up_type=%5
set release_path=%6

echo ==========config:app_repo=%app_repo%,send_apk_sign=%send_apk_sign%,pc_exe=%pc_exe%,apk2cdn=%apk2cdn%,up_type=%up_type%,release_path=%release_path%============

::app升级测试功能
echo app_repo == %app_repo%
if "%app_repo%" == "0" (
    echo app_repo is 0 and meaning apk upgrade test do nothing
)else (
    echo this is apk upgrade building ...
    copy %apk_dir%\*.apk  .\
    :: not find file
    IF ERRORLEVEL 1 exit 1
    for /F %%h  in ( 'dir /B %cur_path%\ ^|findstr ".apk" ' ) do (
        set apk_name=%%h
        echo apk_name=!apk_name!
        ren !apk_name! %result_dir%_!apk_name!
        echo new_name=%result_dir%_!apk_name!
    )
    set apk_new_name=%result_dir%_!apk_name!
    echo apk_new_name=!apk_new_name!
    echo upload file to cibn .....
    ::sender app
    if "%app_repo%" == "1"  (
        %up_dir%\tools\winscp.exe /console /script=%up_dir%\ascp_put_sender_files.txt
        echo pkg_path=%ftp_url%/phoenix_send_app/!apk_new_name!
    )

    ::reciever app
    if "%app_repo%" == "2"  (
        %up_dir%\tools\winscp.exe /console /script=%up_dir%\ascp_put_rec_files.txt
        echo pkg_path=%ftp_url%/phoenix_app/!apk_new_name!
    )
    echo md5sum as follow:
    %md5sum% !apk_new_name!
)

::发送端app签名功能
if "%send_apk_sign%"  == "0" (
    echo send_apk_sign is 0 and meaning sender apk sign do nothing
)else (
    echo this is send_apk_sign building
    copy %up_dir%\tools\mirrorcast_aw.key .\
    echo cur_path=%cur_path% and ftp_path=%ftp_sign_path%
    copy  %local_sign_path%\*.apk    .\
    :: not find file
    IF ERRORLEVEL 1 exit 1
    md tmp_target
    md target
    for /F %%h  in ( 'dir /B %cur_path%\ ^|findstr ".apk"  ' ) do (
        set apk_name=%%h
        echo apk_name=!apk_name!
        echo "============signing !apk_name! ==================="
        call  %up_dir%\sign_tool.sh   !apk_name!
        echo sign log as follow:
        more sign_log.txt
        echo "============signed !apk_name! done==================="

    )
    ping 127.0.0.1
    echo target apk as follow:
    dir /B %cur_path%\tmp_target\ |findstr ".apk"
    echo ftp path info as follow: > ftp_send_info
    for /F %%h  in ( 'dir /B %cur_path%\tmp_target\ ^|findstr ".apk"  ' ) do (
        set signed_apk_name=%%h
        echo signed_apk_name=!signed_apk_name!
        echo ==================zipalign optimize===================
        ::zipalign.exe -v 4 old.apk new.zpk
        %up_dir%\tools\zipalign.exe -v 4 tmp_target\!signed_apk_name! target\!signed_apk_name!
        echo ===============copy !signed_apk_name! to ftp==================== 
        xcopy target\!signed_apk_name!  %ftp_sign_path%
        IF ERRORLEVEL 1 exit 1
        echo ftp_path=%ftp_signed_apk%/!signed_apk_name! >> ftp_send_info
        echo md5sum as follow: >> %cur_path%\ftp_send_info
        cd target
        %md5sum% !signed_apk_name! >> %cur_path%\ftp_send_info
        cd %cur_path%
    )
    more ftp_send_info

)
::pc升级测试功能
if "%pc_exe%" == "0" (
    echo pc_exe is 0 and meaning pc upgrade do nothing
)else (
    echo this is pc  upgrade building ...
    copy %pc_dir%\*.exe  .\
    :: not find file
    IF ERRORLEVEL 1 exit 1
    for /F %%h  in ( 'dir /B %cur_path%\ ^|findstr ".exe" ' ) do (
        set exe_name=%%h
        echo exe_name=!exe_name!
        ren !exe_name! %result_dir%_!exe_name!
        echo new_name=%result_dir%_!exe_name!
    )

    set exe_new_name=%result_dir%_!exe_name!
    echo exe_new_name=!exe_new_name!
    ::压缩exe到zip中，提取exe的除".exe"的前面字段为压缩包的名字，比如201809121730_LeboPCCast_3.08.13.zip
    echo zip_name=!exe_new_name:~0,-4! and zip file .....
    %zip% a -tzip  !exe_new_name:~0,-4!.zip   !exe_new_name!
    echo upload pc file to cibn .....
    %up_dir%\tools\winscp.exe /console /script=%up_dir%\ascp_put_pc_files.txt
    echo pkg_path=%ftp_url_release%/phoenix_pc/!exe_new_name!
    echo md5sum as follow:
    %md5sum% !exe_new_name!
)
::app上传到cdn功能
if "%apk2cdn%" == "0" (
    echo apk2cdn is 0 and meaning apk upgrade test do nothing
)else (
    echo this is apk cdn building ...
    copy %apk2cdn_path%\*.apk  .\
    :: not find file
    IF ERRORLEVEL 1 exit 1
    for /F %%h  in ( 'dir /B %cur_path%\ ^|findstr ".apk" ' ) do (
        set apk_name=%%h
        echo apk_name=!apk_name!
        ren !apk_name! %result_dir%_!apk_name!
        echo new_name=%result_dir%_!apk_name!
    )
    set apk_new_name=%result_dir%_!apk_name!
    echo apk_new_name=!apk_new_name!

    %md5sum% !apk_new_name! > md5sum.txt
    ::将文件中的星号换成空格
    %sed% -i 's/*/ /g' md5sum.txt
    echo md5sum.txt as follow:
    more md5sum.txt
    %zip% a -tzip !apk_new_name:~0,-4!.zip   md5sum.txt !apk_new_name!
    for /F %%k in ( ' dir /B %cur_path%\ ^|findstr ".zip" ' ) do (
                set app_zip=%%k
                echo app_zip=!app_zip!
            )

    ::sender app
    if "%apk2cdn%" == "1"  (
        echo upload file to sender cibn .....
        if "%up_type%" == "apk" (
            %up_dir%\tools\winscp.exe /console /script=%up_dir%\cdn_put_sender_files.txt
            echo pkg_path=%ftp_url_release%/phoenix_send_app/!apk_new_name!
            echo md5sum as follow:
            %md5sum% !apk_new_name!
        )
        if "%up_type%" == "zip" (
            %up_dir%\tools\winscp.exe /console /script=%up_dir%\cdn_put_sender_zip_files.txt
            echo pkg_path=%ftp_url_release%/phoenix_send_app/!app_zip!
            echo zip md5sum as follow:
            %md5sum% !app_zip!
        )

        if "%up_type%" == "0" (
            echo "have no need upload send apk ,and you need select upload type (zip or apk)"
        )
    )

    ::reciever app
    if "%apk2cdn%" == "2"  (
        echo upload file to reciever cibn .....
        if "%up_type%" == "apk" (
            %up_dir%\tools\winscp.exe /console /script=%up_dir%\cdn_put_rec_files.txt
            echo pkg_path=%ftp_url_release%/phoenix_app/!apk_new_name!
            echo md5sum as follow:
            %md5sum% !apk_new_name!
        )
        if "%up_type%" == "zip" (
            %up_dir%\tools\winscp.exe /console /script=%up_dir%\cdn_put_rec_zip_files.txt
            echo pkg_path=%ftp_url_release%/phoenix_app/!app_zip!
            echo zip md5sum as follow:
            %md5sum% !app_zip!
        )
        if "%up_type%" == "0" (
            echo "have no need upload send apk ,and you need select upload type (zip or apk)
        )
    )
)

::上传版本发布说明到ftp版本发布路径下功能
if "%release_path%" == "0" (
    echo release_path is 0 and meaning do nothing
) else (
    echo this is upload release note building ...
    copy %release_dir%\*.txt  .\
    :: not find file
    IF ERRORLEVEL 1 exit 1
    for /F %%h  in ( 'dir /B %cur_path%\ ^|findstr ".txt" ' ) do (
        set exe_name=%%h
        echo exe_name=!exe_name!
    )
    echo exe_new_name=!exe_name!
    if not "!exe_name!" == "release_note.txt" (
        echo your file name must be release_note.txt
        exit 1
    )

    echo upload release note file to %release_path%
    call  %up_dir%\upload_release_note.sh   %release_path%
    echo release path=%release_path%
)

echo Done